<template>
  <div class="viedodetails">
    <div class="video-box">
      <div class="videopaly">
        <h3>视频详情</h3>
        <!-- 视频播放器 -->
        <ch-videoplay
          :title="videoInfo.title"
          :cover="videoInfo.coverUrl"
          :url="videoUrl.url"
        />
        <!-- 作者头像 -->
        <div class="author">
          <div class="avatar">
            <img :src="videoInfo.avatarUrl" alt="" />
            <span v-if="videoInfo !== ''">{{
              videoInfo.creator.nickname
            }}</span>
          </div>
          <el-button style="border-radius: 20px" size="mini" type="primary"
            >+关注</el-button
          >
        </div>
        <!-- 标题 -->
        <h1 class="video-title">{{ videoInfo.title }}</h1>
        <!-- 发布时间 播放次数 -->
        <div class="date-play">
          <p>发布：{{ videoInfo.publishTime | dateTime }}</p>
          <p>播放：{{ videoInfo.praisedCount | playCount }}</p>
        </div>
        <!-- 视频标签 -->
        <div class="video-tag">
          <span
            v-for="(item, index) of videoInfo.videoGroup"
            :key="index"
            @click="toVideoList(item.name)"
            >{{ item.name }}</span
          >
        </div>
        <!-- 按钮 -->
        <div class="btns">
          <el-button size="medium">
            <i class="iconfont icon-zanpress"></i> 点赞({{
              videoInfo.praisedCount
            }})</el-button
          >
          <el-button size="medium">
            <i class="iconfont icon-tianjiawenjianjia"></i>收藏({{
              videoInfo.subscribeCount
            }})</el-button
          >
          <el-button size="medium">
            <i class="iconfont icon-fenxiang1"></i>分享({{
              videoInfo.shareCount
            }})</el-button
          >
        </div>
      </div>
      <div class="recommended">
        <h3>相关推荐</h3>
        <div class="video-item" v-for="(item, index) of recommend" :key="index">
          <div class="image" @click="toVideoDetails(item.data.vid)">
            <img
              :src="item.data.coverUrl"
              :alt="item.data.title"
              :title="item.data.title"
            />
            <div class="mask">
              <span>
                <i class="iconfont icon-bofang5"></i
                >{{ item.data.playTime | playCount }}</span
              >
            </div>
            <div class="mask-time">
              <span>{{ item.data.durationms | playtime }}</span>
            </div>
          </div>
          <div class="name">
            <p :title="item.data.title" @click="toVideoDetails(item.data.vid)">
              {{ item.data.title }}
            </p>
            <p
              v-if="item.data.creator !== undefined"
              :title="item.data.creator.nickname"
            >
              {{ item.data.creator.nickname }}
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getVideoDetails, getVideoUrl, getVideorecommend } from "@/api/video";
import VideoPlay from "@/components/common/videoplay";
export default {
  name: "viedodetails",
  components: {
    "ch-videoplay": VideoPlay
  },
  props: {},
  data() {
    return {
      videoInfo: "", // 视频详情
      videoUrl: "", // 视频地址
      recommend: [], // 推荐视频
      index: 1
    };
  },
  computed: {
    tokenCookie() {
      return this.$store.state.tokenCookie;
    }
  },
  watch: {
    $route() {
      // 获取视频详情
      this.getVideoDetails();
      // 获取视频播放地址
      this.getVideoUrl();
      this.getVideorecommend();
    }
  },
  created() {
    // 获取视频详情
    this.getVideoDetails();
    // 获取视频播放地址
    this.getVideoUrl();
    // 获取相关推荐
    this.getVideorecommend();
  },
  mounted() {},
  methods: {
    async getVideoDetails() {
      this.showLoading();
      const data = await getVideoDetails({
        id: this.$route.query.id
      });
      if (data.data.code == 200) {
        this.closeLoading();
      }
      this.videoInfo = data.data.data;
      this.title();
    },
    // 获取视频地址
    async getVideoUrl() {
      const data = await getVideoUrl({
        id: this.$route.query.id
      });
      this.videoUrl = data.data.urls[0];
    },
    // 获取相关推荐视频
    async getVideorecommend() {
      this.recommend = [];
      const data = await getVideorecommend({
        id: this.$route.query.id,
        offset: 0,
        cookie: this.tokenCookie.cookie
      });
      // let arr = [];
      data.data.datas.forEach(val => {
        if (val.data.liveData === undefined) {
          this.recommend.push(val);
        }
      });
    },
    title() {
      document.title = this.videoInfo.title;
    },
    toVideoDetails(id) {
      this.index++;
      this.$router.push({
        name: "videodetails",
        query: {
          id
        }
      });
    },
    // 跳转
    toVideoList(cat) {
      this.$router.push({
        name: "videolist",
        query: {
          cat
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.viedodetails {
  padding: 20px 50px;
  margin-bottom: 80px;
  h3 {
    margin-bottom: 20px;
  }
  .video-box {
    display: flex;
    justify-content: space-around;
    .videopaly {
      .author {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 50px;
        .avatar {
          height: 100%;
          img {
            height: 100%;
            border-radius: 50%;
            margin-right: 15px;
          }
          span {
            font-size: 16px;
          }
        }
      }
      .video-title {
        margin-top: 30px;
        font-size: 22px;
        color: #000;
        font-weight: 700;
      }
      .date-play {
        display: flex;
        justify-content: flex-start;
        p {
          margin-right: 15px;
          padding: 15px 0;
          font-size: 12px;
          color: #ccc;
        }
      }
      .video-tag {
        display: flex;
        span {
          padding: 2px 10px;
          margin-right: 10px;
          height: 20px;
          line-height: 20px;
          align-items: center;
          font-size: 12px;
          background: #f9f9f9;
          border-radius: 10px;
          cursor: pointer;
        }
      }
      .btns {
        display: flex;
        justify-content: flex-start;
        margin-right: 15px;
        padding: 15px 0;
        .el-button {
          border-radius: 20px;
        }
      }
    }
    .recommended {
      padding: 0 20px;
      width: 35%;
      .video-item {
        display: flex;
        align-items: center;
        height: 110px;
        width: 500px;
        margin-bottom: 10px;
        .image {
          position: relative;
          height: 100%;
          width: 200px;
          cursor: pointer;
          img {
            height: 100%;
            width: 100%;
            border-radius: 5px;
          }
          .mask {
            position: absolute;
            top: 0;
            right: 0;
            height: 20px;
            width: 100%;
            span {
              float: right;
              padding: 0 5px;
              font-size: 12px;
              color: #fff;
              .icon-bofang5 {
                font-size: 12px;
              }
            }
          }
          .mask-time {
            position: absolute;
            left: 0;
            bottom: 0;
            height: 20px;
            width: 100%;
            span {
              float: right;
              padding: 0 5px;
              font-size: 12px;
              color: #fff;
            }
          }
        }
        .name {
          width: 200px;
          padding: 0 10px;
          p {
            padding: 5px 0;
            &:nth-child(1) {
              font-size: 14px;
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 2;
              overflow: hidden;
              cursor: pointer;
            }
            &:nth-child(2) {
              font-size: 12px;
              color: #ccc;
              cursor: pointer;
            }
          }
        }
      }
    }
  }
}
</style>
